class Node{
    constructor(element){
        this.element = element;
        this.left = null;
        this.right = null;
    }
}

class Tree{
    constructor(){
        this.root = null;
    }
    //插入
    insert(element){
        let node = new Node(element)

        if(this.root === null){
            this.root = node;
        }else{
            //树不为空
            this.insertNode(node,this.root)
        }
    }
    insertNode(newNode , rootNode){
        if(newNode.element < rootNode.element){
            //左边
            if(rootNode.left === null ){    
                rootNode.left = newNode
            }else{
                this.insertNode(newNode,rootNode.left)
            }
        }
        else{
            //右边
            if(rootNode.right === null ){    
                rootNode.right = newNode
            }else{
                this.insertNode(newNode,rootNode.right)
            }
        }
    }
    //查找最小值
    min(){
        return this.minNode(this.root)
    }
    minNode(rootNode){
        while(rootNode.left != null){
            rootNode = rootNode.left
        }
        return rootNode
    }
    //最大值
    max(){
        return this.maxNode(this.root)
    }
    maxNode(rootNode){
        while(rootNode.right != null){
            rootNode = rootNode.right
        }
        return rootNode
    }
    //查找
    search(element){
        return this.searchNode(element,this.root)
    }
    searchNode(element,rootNode){
        if(this.root === null){
            return false
        }
        if(element < rootNode.element){
            return this.searchNode(element.rootNode)
        }else if(element > rootNode.element){
            return this.searchNode(element,rootNode)
        }else{
            return true
        }
    }
}